package com.axiu;

import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends BaseActivity implements OnClickListener {

    private HomeFragment homeFragment;
    private AccountFragment accountFragment;
    private SettingsFragment settingsFragment;
    private ShareFragment shareFragment;

    private ImageView homeImg;
    private ImageView accountImg;
    private ImageView settingsImg;
    private ImageView shareImg;

    private TextView homeTxt;
    private TextView accountTxt;
    private TextView settingsTxt;
    private TextView shareTxt;
    private AlertDialog myDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout home = (LinearLayout) findViewById(R.id.home);
        LinearLayout account = (LinearLayout) findViewById(R.id.account);
        LinearLayout settings = (LinearLayout) findViewById(R.id.settings);
        LinearLayout share = (LinearLayout) findViewById(R.id.share);
        Button back = (Button) findViewById(R.id.top_bar_back);

        homeImg = (ImageView) findViewById(R.id.home_img);
        accountImg = (ImageView) findViewById(R.id.account_img);
        settingsImg = (ImageView) findViewById(R.id.settings_img);
        shareImg = (ImageView) findViewById(R.id.share_img);

        homeTxt = (TextView) findViewById(R.id.home_txt);
        accountTxt = (TextView) findViewById(R.id.account_txt);
        settingsTxt = (TextView) findViewById(R.id.settings_txt);
        shareTxt = (TextView) findViewById(R.id.share_txt);

        home.setOnClickListener(this);
        account.setOnClickListener(this);
        settings.setOnClickListener(this);
        share.setOnClickListener(this);
        setDefaultFradment();
        TextView username = (TextView) findViewById(R.id.username);
        //username.setVisibility(View.GONE);
        back.setVisibility(View.GONE);
    }

    private void setDefaultFradment() {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        homeFragment = new HomeFragment();
        transaction.replace(R.id.content, homeFragment);
        resetClick();
        homeImg.setSelected(true);
        homeTxt.setSelected(true);
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        changeMenu(view.getId());
    }

    private void resetClick() {
        homeImg.setSelected(false);
        homeTxt.setSelected(false);
        accountImg.setSelected(false);
        accountTxt.setSelected(false);
        settingsImg.setSelected(false);
        settingsTxt.setSelected(false);
        shareImg.setSelected(false);
        shareTxt.setSelected(false);
    }

    private void changeMenu(int id){
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        resetClick();
        switch (id) {
            case R.id.home:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                transaction.replace(R.id.content, homeFragment);
                homeImg.setSelected(true);
                homeTxt.setSelected(true);
                break;
            case R.id.account:
                if (accountFragment == null) {
                    accountFragment = new AccountFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                transaction.replace(R.id.content, accountFragment);
                accountImg.setSelected(true);
                accountTxt.setSelected(true);
                break;
            case R.id.settings:
                if (settingsFragment == null) {
                    settingsFragment = new SettingsFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                transaction.replace(R.id.content, settingsFragment);
                settingsImg.setSelected(true);
                settingsTxt.setSelected(true);
                break;
            case R.id.share:
                if (shareFragment == null) {
                    shareFragment = new ShareFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                transaction.replace(R.id.content, shareFragment);
                shareImg.setSelected(true);
                shareTxt.setSelected(true);
                showShareDialog();
                break;
            default:
                ;
        }
        transaction.commit();
    }

    private void showShareDialog(){
        myDialog = new AlertDialog.Builder(MainActivity.this).create();
        myDialog.setCancelable(false);
        myDialog.show();
        myDialog.getWindow().setContentView(R.layout.share_dialog);
        myDialog.getWindow().findViewById(R.id.dialog_confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView text = (TextView) myDialog.getWindow().findViewById(R.id.share_content);
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_TEXT, text.getText());
                shareIntent.setType("text/plain");
                startActivity(Intent.createChooser(shareIntent, "分享到"));
            }
        });
        myDialog.getWindow().findViewById(R.id.dialog_cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.cancel();
                changeMenu(R.id.home);
            }
        });
    }
}
